﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hutech.Authentication.Library.Web.Framework;
using Resources;
using Hutech.Authentication.Library.Business.WebHelpers;
using log4net;
using Hutech.Authentication.Library.Business;
using System.Globalization;
using nvn.Library.Frameworks;

namespace Hutech.CRM.Web.Admin
{
    public partial class PermissionManager : hutechBasePage
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(RoleManager));
        private static bool debugLog = log.IsDebugEnabled;

        protected string EditPropertiesImage = "~/Data/SiteImages/" + WebConfigSettings.EditPropertiesImage;
        protected string DeleteLinkImage = "~/Data/SiteImages/" + WebConfigSettings.DeleteLinkImage;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //this.btnAddRole.Click += new EventHandler(btnAddRole_Click);
            //this.ButtonAddModule.Click += new EventHandler(ButtonAddModule_Click);
            //UIHelper.AddConfirmationDialog(ButtonAddModule, "Chức năng chưa hoàn thiện. Vui lòng liên hệ người lập trình...");

            this.DataListAction.ItemCommand += new DataListCommandEventHandler(DataListAction_ItemCommand);
            this.DataListAction.ItemDataBound += new DataListItemEventHandler(DataListAction_ItemDataBound);

            this.LinkButtonThemMoi.Click += new EventHandler(LinkButtonThemMoi_Click);

            PopupAddNewItemPermission1.ReloadData += new EventHandler(PopupAddNewItemPermission1_ReloadData);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!WebUser.IsAdminOrRoleAdmin)
            {
                SiteUtils.RedirectToAccessDeniedPage(this);
                return;
            }
            PopulateLabels();
            PopulateControls();
        }

        #region Load All data
        private void PopulateControls()
        {
            if (Page.IsPostBack)
                return;
            BindModuleList();
        }

        private void PopulateLabels()
        {
            Title = Resource.FeaturePermissions;
            spnTitle.InnerText = Resource.FeaturePermissions;
            //LinkButtonAddNewModule.Text = Resource.ModuleDefsAddButton;
            //LinkButtonAddNewModule.ToolTip = Resource.ModuleDefsAddButton;
            //ButtonAddModule.Text = "Thêm mới";
            //ButtonAddModule.ToolTip = "Thêm mới";


            lnkAdminMenu.Text = Resource.AdminMenuLink;
            lnkAdminMenu.ToolTip = Resource.AdminMenuLink;
            lnkAdminMenu.NavigateUrl = SiteRoot + "/Admin/AdminMenu.aspx";

            //lnkRoleAdmin.Text = Resource.AdminMenuRoleAdminLink;
            //lnkRoleAdmin.ToolTip = Resource.AdminMenuRoleAdminLink;
            //lnkRoleAdmin.NavigateUrl = SiteRoot + "/Admin/RoleManager.aspx";

            //SiteUtils.SetButtonAccessKey(btnAddRole, AccessKeys.RolesAddButtonAccessKey);
        }

        private void BindModuleList()
        {
            DataListAction.DataSource = Actions.GetAll();
            DataListAction.DataBind();
        }
        #endregion
        #region All event
        void PopupAddNewItemPermission1_ReloadData(object sender, EventArgs e)
        {
            BindModuleList();
            pnlMain.Update();
        }
        void LinkButtonThemMoi_Click(object sender, EventArgs e)
        {
            PopupAddNewItemPermission1.Show(Guid.Empty);
        }
        protected void DataListAction_ItemCommand(object sender, DataListCommandEventArgs e)
        {
            if (debugLog)
            {
                log.Debug("fired event RolesList_ItemCommand");
            }

            Guid actionGuid = new Guid(DataListAction.DataKeys[e.Item.ItemIndex].ToString());
            Actions action = new Actions(actionGuid);
            switch (e.CommandName)
            {
                case "edit":
                    DataListAction.EditItemIndex = e.Item.ItemIndex;
                    BindModuleList();
                    break;

                case "apply":
                    action.ActionName = ((TextBox)e.Item.FindControl("TextBoxActionName")).Text;
                    action.ActionCommand = ((TextBox)e.Item.FindControl("TextBoxActionCommand")).Text;
                    action.Description = ((TextBox)e.Item.FindControl("TextBoxActionDecription")).Text;
                    action.Save();
                    DataListAction.EditItemIndex = -1;
                    BindModuleList();
                    break;

                case "delete":
                    //Cho nay se lam sau... Cho nay phai kiem tra dieu kien role do co con cap quyen cho user nao hay khong
                    //Hoac kiem tra khong cho xoa cac quyen dac biet.  ................Giai phap?

                    //if (role.CanBeDeleted(WebConfigSettings.RolesThatCannotBeDeleted.SplitOnChar(';')))
                    //{
                    //    Role.DeleteRole(roleID);
                    //    rolesList.EditItemIndex = -1;
                    //}
                    Actions.Delete(actionGuid);
                    BindModuleList();
                    break;
                case "cancel":
                    DataListAction.EditItemIndex = -1;
                    BindModuleList();
                    //WebUtils.SetupRedirect(this, Request.RawUrl);
                    break;
            }
        }
        void DataListAction_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ImageButton btnDelete = e.Item.FindControl("btnDelete") as ImageButton;
            UIHelper.AddConfirmationDialog(btnDelete, Resource.RolesDeleteWarning);

            //ImageButton btnEdit = e.Item.FindControl("btnEdit") as ImageButton;

            //AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
            //trigger.ControlID = btnEdit.ClientID;
            //trigger.EventName = "Click";
            //pnlMain.Triggers.Add(trigger);
        }
        #endregion
    }
}
